package com.auth.controller;


import com.common.ano.Log;
import com.common.dto.LoginFormDTO;
import com.auth.domain.dto.RegisterFormDTO;
import com.auth.domain.dto.UpdateUserDTO;
import com.auth.domain.entity.User;
import com.common.vo.RegisterVO;
import com.common.vo.Result;
import com.auth.domain.vo.UserInfoVO;
import com.auth.service.IUserService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author 坤哥
 * @since 2025-10-08
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Resource
    private IUserService userService;

    /**
     * 用户注册
     */
    @PostMapping("/register")
    @Log(operationType = "注册", operationModule = "用户管理", operationDesc = "用户注册")
    public Result<RegisterVO> register(@RequestBody RegisterFormDTO registerFormDTO){
        log.info("用户注册信息为：{}",registerFormDTO);
        RegisterVO registerVO = userService.register(registerFormDTO);
        return Result.ok(registerVO);
    }

    /**
     * 用户登录
     */
    @PostMapping("/login")
    @Log(operationType = "登录", operationModule = "用户管理", operationDesc = "用户登录")
    public Result<String> login(@RequestBody LoginFormDTO loginFormDTO){
        log.info("用户登录信息为：{}",loginFormDTO);
        String token = userService.login(loginFormDTO);
        return Result.ok(token);
    }

    /**
     * 用户登出
     */
    @GetMapping("/logout")
    @Log(operationType = "登出", operationModule = "用户管理", operationDesc = "用户登出")
    public Result logout(){
        log.info("用户执行退出操作：");
        userService.logout();
        return Result.ok();
    }

    /**
     * 获取用户信息
     */
    @GetMapping("/info")
    public Result<UserInfoVO> getUserInfo(){
        log.info("获取登录的用户信息:");
        UserInfoVO userInfoVO = userService.getUserInfo();
        return Result.ok(userInfoVO);
    }

    /**
     * 获取用户列表（分页）
     */
    @GetMapping("/list")
    @Log(operationType = "查询", operationModule = "用户管理", operationDesc = "获取用户列表")
    public Result<IPage<User>> getUserList(
            @RequestParam(required = false) String username,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize
    ) {
        log.info("获取用户列表: username={}, pageNum={}, pageSize={}", username, pageNum, pageSize);
        IPage<User> page = userService.getUserList(username, pageNum, pageSize);
        return Result.ok(page);
    }

    /**
     * 更新用户信息
     */
    @PutMapping("/update")
    @Log(operationType = "更新", operationModule = "用户管理", operationDesc = "更新用户信息")
    public Result<String> updateUser(@RequestBody UpdateUserDTO updateUserDTO) {
        log.info("更新用户信息: {}", updateUserDTO);
        userService.updateUser(updateUserDTO);
        return Result.ok("更新成功");
    }

    /**
     * 删除用户（软删除）
     */
    @DeleteMapping("/delete/{id}")
    @Log(operationType = "删除", operationModule = "用户管理", operationDesc = "删除用户")
    public Result<String> deleteUser(@PathVariable Long id) {
        log.info("删除用户: id={}", id);
        userService.deleteUser(id);
        return Result.ok("删除成功");
    }

    /**
     * openFeign根据用户名获取用户id
     */
    @GetMapping("/aopGetByUsername")
    public Long getByUsername(@RequestParam("username") String username){
        log.info("openFeign根据用户名获取用户id:");
        return userService.getByUsername(username);
    }

    /**
     * openFeign根据用户id获取用户名
     */
    @GetMapping("/aopGetByUserId")
    public String getByUserId(@RequestParam("userId") Long userId){
        log.info("openFeign根据用户id获取用户名:");
        return userService.getByUserId(userId);
    }
}
